﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsForms_Logic_Questions_Generator.Operators;
using WindowsForms_Logic_Questions_Generator;
using System.IO;

namespace WindowsForms_Logic_Questions_Generator
{
    class RandomQuestionsGenerator
    {
        public string[,] m_Test_Template;
        private FormatModulator m_Modulator = new FormatModulator();
        private PrinterModulator m_Printer = new PrinterModulator();
        private Random rnd = new Random();


        public void m_to_string(int numOfTests, DocumentExtraData extraData, ListQestions questionsList, int font0, int font1, int font2, int font3, int font4, int font5, int font6, string appPath, bool CheckBoxPrintToDefault)
        {
            m_Test_Template = new string[numOfTests, 7];


            for (int index = 0; index <= numOfTests - 1; index++)
            {
                m_Test_Template[index, 0] = "\t\t\t\t\t" + extraData.GetDateData() + "\n\r\n\r";
                m_Test_Template[index, 1] = "\t\t" + extraData.GetHeaderData() + "\n\r\n\r";
                m_Test_Template[index, 2] = extraData.GetStudentNameData() + "\n\r";
                m_Test_Template[index, 3] = extraData.GetExplanationData() + "\n\r";
                int a = questionsList.HowMany();

                int random = 0, questionNum = 1;
                for (int i = 0; i < a; i++)
                {
                    int how_many_range_max = (int)questionsList.ElementAt(i).HowLong();
                    int[] indexRange = new int[how_many_range_max];
                    if (questionsList.ElementAt(i).ElementAt(0).ToString().Contains("#comment:#") != true)
                    {
                        m_Test_Template[index, 4] += (questionNum++).ToString() + ")";
                    }
                    questionsList.ElementAt(i).FindRange(indexRange);
                    for (int qindex = 0; qindex < how_many_range_max; qindex++)
                    {
                        if (indexRange[qindex] != 1)
                            m_Test_Template[index, 4] += questionsList.ElementAt(i).ElementAt(qindex).ToString();
                        else
                        {
                            random = m_Random((RangeButton)questionsList.ElementAt(i).ElementAt(qindex));
                            if (qindex == 0)
                                m_Test_Template[index, 4] += " ";
                            m_Test_Template[index, 4] += random;
                        }
                    }
                    m_Test_Template[index, 4] += "\n\r\n\r";
                }
                m_Test_Template[index, 5] = "\n\r" + extraData.GetFinalExplanationData();
                m_Test_Template[index, 6] = "\n\r" + extraData.GetGoodLuckData();

                m_Test_Template[index, 4] = m_Test_Template[index, 4].Replace("{+}", "+");
                m_Test_Template[index, 4] = m_Test_Template[index, 4].Replace("{*}", "*");
                m_Test_Template[index, 4] = m_Test_Template[index, 4].Replace("{/}", ":");
                m_Test_Template[index, 4] = m_Test_Template[index, 4].Replace("{=}", "=");
                m_Test_Template[index, 4] = m_Test_Template[index, 4].Replace("{-}", "-");
                m_Test_Template[index, 4] = m_Test_Template[index, 4].Replace(" #comment:#  :  ", "");

            }
            
            for (int i = 0; i <= numOfTests - 1; i++)
            {

                string[] tmpString = new string[7];
                for (int j = 0; j < 7; j++)
                {
                    tmpString[j] = replaceNewLine(m_Test_Template[i, j]);

                }

                String appNewPath = appPath + "_" + i + ".rtf";
                m_Modulator.createRtf(tmpString, font0, font1, font2, font3, font4, font5, font6, appNewPath);
                
                if (CheckBoxPrintToDefault)
                {
                    System.Windows.Forms.RichTextBox richTextBox1 = new System.Windows.Forms.RichTextBox();
                    richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
                    richTextBox1.Location = new System.Drawing.Point(0, 24);
                    richTextBox1.Name = "richTextBox1";
                    richTextBox1.Size = new System.Drawing.Size(534, 338);
                    richTextBox1.TabIndex = 1;
                    richTextBox1.Text = "";
                    richTextBox1.LoadFile(appNewPath);
                    bool printcheck = m_Printer.m_PrintTest(richTextBox1);
                    if (printcheck == false)
                    {
                        MessageBox.Show("Couldn't print the " + (i + 1).ToString() + " test.\n Plese try again later");
                    }
                }
            }
        }
        private int m_Random(RangeButton target)
        {
            int from = target.from, to = target.to, x = 0;
            int max, min;
            max = Math.Max(from, to);
            min = Math.Min(from, to);
            x = rnd.Next(min, max + 1);
            if (x != 0)
                return x;
            else if (to >= 0)
                return 1;
            else return -1;
        }

        private string replaceNewLine(string st)
        {
            st = st.Replace("\r\n", "\\line ");
            st = st.Replace("\n", "\\line ");
            st = st.Replace("\r", "\\line ");

            return st;
        }
        public bool GenerateRandomsQuestions(DocumentExtraData extraData, ListQestions questionsList, string appPath)
        {
            if (questionsList.HowMany() == 0)
            {
                MessageBox.Show("No template could be found!! Please create a template and try again");
                return false;
            }
            int font0 = 12, font1 = 18, font2 = 12, font3 = 12, font4 = 12, font5 = 12, font6 = 16, numOfTests = 1;
            bool CheckBoxPrintToDefault = false;
            using (Form2 print = new Form2())
            {
                Exception zero = new Exception();
                if (print.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        CheckBoxPrintToDefault = print.ArePrinting();
                        print.ClearCheckBoxPrintToDefault();
                        numOfTests = Convert.ToInt32(print.HowMany);
                        if (numOfTests <= 0)
                            numOfTests = 1;
                        font0 = Convert.ToInt32(print.font0);
                        if (font0 <= 0 || font0 > 36)
                            throw zero;
                        font1 = Convert.ToInt32(print.font1);
                        if (font1 <= 0 || font1 > 36)
                            throw zero;
                        font2 = Convert.ToInt32(print.font2);
                        if (font2 <= 0 || font2 > 36)
                            throw zero;
                        font3 = Convert.ToInt32(print.font3);
                        if (font3 <= 0 || font3 > 36)
                            throw zero;
                        font4 = Convert.ToInt32(print.font4);
                        if (font4 <= 0 || font4 > 36)
                            throw zero;
                        font5 = Convert.ToInt32(print.font5);
                        if (font5 <= 0 || font5 > 36)
                            throw zero;
                        font6 = Convert.ToInt32(print.font6);
                        if (font6 <= 0 || font6 > 36)
                            throw zero;

                    }
                    catch (FormatException e)
                    {
                        MessageBox.Show("Input string is not a sequence of digits.Please try again");
                        return false;
                    }
                    catch (OverflowException e)
                    {
                        MessageBox.Show("One or more of the numbers are too big.Please try again");
                        return false;
                    }
                    catch
                    {
                        MessageBox.Show("One or more of the numbers are  are not in range.Please try again");
                        return false;
                    }


                }

            }
            m_to_string(numOfTests, extraData, questionsList, font0, font1, font2, font3, font4, font5, font6, appPath, CheckBoxPrintToDefault);
            return true;

        }
    }
}
